﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Component;
using NHibernate.Criterion;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Component.SystemSettingComponent;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class CourseController : ControllerBase
    {
        CourseComponent courseComponent = new CourseComponent();
        OrganizationComponent organizationComponent = new OrganizationComponent();
        UserComponent userComponent = new UserComponent();
        UserCourseComponent userCourseComponent = new UserCourseComponent();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            IList<User> users =new List<User>();
            users = userComponent.FindAll();
            TempData["Users"] = users;
            return View();
        }


        [HttpPost]
        public void Create(string Name,string IsActive,string Account)
        {
            
            Course entity = new Course();
            entity.Name=Name;
            entity.IsActive= bool.Parse(IsActive);
            entity.Create();
            User user = userComponent.FindAll().Where(o => o.Account == Account).ToList()[0];
            UserCourse userCourse = new UserCourse();
            userCourse.User = user;
            userCourse.Course = entity;
            userCourse.Create();
        }

        public ActionResult Update(int id)
        {
            IList<User> users = new List<User>();
            users = userComponent.FindAll();
            TempData["Users"] = users;
            Course course = courseComponent.FindByID(id);
            if (course.UserCourses.Count != 0)
            {
                TempData["User"] = course.UserCourses[0].User.Account;
            }
            return View();
        }

        [HttpPost]
        public void Update(int id, string Name, string IsActive, string Account)
        {
            Course entity = courseComponent.FindByID(id);
            entity.Name = Name;
            entity.IsActive = bool.Parse(IsActive);
            entity.Update();
            if (entity.UserCourses.Count == 0)
            {
                if (Account != "---请选择---")
                {
                    User user = userComponent.FindAll().Where(o => o.Account == Account).ToList()[0];
                    UserCourse userCourse = new UserCourse();
                    userCourse.User = user;
                    userCourse.Course = entity;
                    userCourse.IsCharge = true;
                    userCourse.Create();
                }
            }
            else
            {
                entity.UserCourses[0].User = userComponent.FindAll().Where(o => o.Account == Account).ToList()[0];
                entity.UserCourses[0].IsCharge = true;
                entity.UserCourses[0].Update();
            }
            AppHelper.LoginedUser = userComponent.FindByID(AppHelper.LoginedUser.ID);
        }

        public string GetEntityByID(int id)
        {
            Course entity = courseComponent.FindByID(id);
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            //list.Add(new KeyValuePair<string, InvokeMethod>("xx", GetUsers));
            string str = entity.ToJsonString<Course>(list);
            return str;
        }

        [HttpPost]
        public void Withdraw(int id)
        {
            Course entity =  courseComponent.FindByID(id);
            if (entity.IsActive == true)
            {
                entity.IsActive = false;
            }
            else
            {
                entity.IsActive = true;
            }
            entity.Update();
        }

        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Course> list = new List<Course>();
            IList<Course> users = courseComponent.GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Course entity in users)
            {
                entity.ToJaonResult<Course>();
                list.Add(entity);
            }
            list.Add(new Course() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }
    }
}
